package socketdemo.bio;

import java.io.*;
import java.net.Socket;

/**
 * @author zoush
 * @createTime 2024/12/04  15:11
 */
public class ChatClientDemo {

    private BufferedReader reader;
    private BufferedWriter writer;
    private Socket socket;

    private static final String HOST = "127.0.0.1";

    private static final int PORT = 8888;

    //发送消息给服务器
    public void sendToServer(String msg) throws Exception {
        //发送之前，判断socket的输出流是否关闭
        if (!socket.isOutputShutdown()) {
            //如果没有关闭就把用户键入的消息放到writer里面
            writer.write(msg + "\n");
            writer.flush();
        }
    }

    /**
     * 接受消息
     *
     * @throws Exception
     */
    public String receive() throws Exception {
        String msg = null;
        if (!socket.isInputShutdown()) {
            msg = reader.readLine();
        }
        return msg;
    }
    //和服务创建连接

    public void start() {
        try {
            socket = new Socket(HOST, PORT);
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            //新建一个线程去监听用户输入的消息
            new Thread(new UserInputHandler(this)).start();
            /**
             * 不停的读取服务器转发的其他客户端的信息
             * 记录一下之前踩过的小坑：
             * 这里一定要创建一个msg接收信息，如果直接用receive()方法判断和输出receive()的话会造成有的消息不会显示
             * 因为receive()获取时，在返回之前是阻塞的，一旦接收到消息才会返回，也就是while这里是阻塞的，一旦有消息就会进入到while里面
             * 这时候如果输出的是receive(),那么上次获取的信息就会丢失，然后阻塞在System.out.println
             * */
            String msg = null;
            while ((msg = receive()) != null) {
                System.out.println(msg);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try{
                writer.close();

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        new ChatClientDemo().start();
    }
}
